#include "collection.h"
#include "filehandler.h"
#include "../database/database.h"
#include "../database/dbalbum.h"
#include "../database/dbartist.h"
#include "../database/dbgenre.h"
#include "../database/dbtrack.h"
#include "preference.h"

#include <QDebug>

Collection::Collection(QObject *parent)
    : QThread(parent)
{
    abort = false;
    DataBase::getAll(*this);
}

void Collection::addCollection()
{
    QMutexLocker locker(&mutex);
    if (!isRunning())
    {
        start();
    }
}

void Collection::run()
{
    mutex.lock();
    Preference *pref = Preference::getInstance();
    QSet<DtLibrary> sLib = pref->getLibrary();
    for(QSet<DtLibrary>::ConstIterator it = sLib.constBegin();
        it != sLib.constEnd(); it ++)
    {
        if(!(*it).getLoad())
        {
            FileHandler::getMusic((*it).getLibrary(), *this);
            DbArtist::create(sArtist);
            DbGenre::create(sGenre);
            DbAlbum::create(sAlbum);
            DbTrack::create(sTrack);
        }
        if(abort)
            return;
    }
    mutex.unlock();
    emit ready();
}

QList<DtAlbum *> & Collection::getAlbums()
{
    return sAlbum;
}

QList<DtArtist *> & Collection::getArtists()
{
    return sArtist;
}

QList<DtTrack *> & Collection::getTracks()
{
    return sTrack;
}

QList<DtGenre *> & Collection::getGenres()
{
    return sGenre;
}

void Collection::test()
{
}

DtArtist * Collection::getArtistId(const int idArt)
{
    QList<DtArtist *>::iterator begin = sArtist.begin();
    QList<DtArtist *>::iterator end = sArtist.end();
    while(end-- != begin)
        if((*end)->getId() == idArt)
            return (*end);
    return NULL;
}

DtAlbum * Collection::getAlbumId(const int idAlb)
{
    QList<DtAlbum *>::iterator begin = sAlbum.begin();
    QList<DtAlbum *>::iterator end = sAlbum.end();
    while(end-- != begin)
        if((*end)->getId() == idAlb)
            return (*end);
    return NULL;
}

DtGenre * Collection::getGenreId(const int idGen)
{
    QList<DtGenre *>::iterator begin = sGenre.begin();
    QList<DtGenre *>::iterator end = sGenre.end();
    while(end-- != begin)
        if((*end)->getId() == idGen)
            return (*end);
    return NULL;
}

DtGenre * Collection::getGenre(const DtGenre *genre)
{
    QList<DtGenre *>::iterator begin = sGenre.begin();
    QList<DtGenre *>::iterator end = sGenre.end();
    while(end-- != begin)
        if(*(*end) == *genre)
            return (*end);
    return NULL;
}

DtAlbum * Collection::getAlbum(const DtAlbum *album)
{
    QList<DtAlbum *>::iterator begin = sAlbum.begin();
    QList<DtAlbum *>::iterator end = sAlbum.end();
    while(end-- != begin)
        if(*(*end) == *album)
            return (*end);
    return NULL;
}

DtArtist * Collection::getArtist(const DtArtist *artist)
{
    QList<DtArtist *>::iterator begin = sArtist.begin();
    QList<DtArtist *>::iterator end = sArtist.end();
    while(end-- != begin)
        if(*(*end) == *artist)
            return (*end);
    return NULL;
}

bool Collection::addArtist(DtArtist *artist)
{
    QList<DtArtist *>::iterator begin = sArtist.begin();
    QList<DtArtist *>::iterator end = sArtist.end();
    while(end-- != begin)
        if(*(*end) == *artist)
            return false;
    sArtist.append(artist);
    return true;
}

bool Collection::addAlbum(DtAlbum *album)
{
    QList<DtAlbum *>::iterator begin = sAlbum.begin();
    QList<DtAlbum *>::iterator end = sAlbum.end();
    while(end-- != begin)
        if(*(*end) == *album)
            return false;
    sAlbum.append(album);
    return true;
}

bool Collection::addGenre(DtGenre *genre)
{
    QList<DtGenre *>::iterator begin = sGenre.begin();
    QList<DtGenre *>::iterator end = sGenre.end();
    while(end-- != begin)
        if(*(*end) == *genre)
            return false;
    sGenre.append(genre);
    return true;
}

void Collection::addTrack(DtTrack *track, DtAlbum *album, DtArtist *artist,
                          DtGenre *genre)
{
    QList<DtTrack *>::iterator begin = sTrack.begin();
    QList<DtTrack *>::iterator end = sTrack.end();
    while(end-- != begin)
        if(*(*end) == *track)
        {
            delete track;
            return;
        }
    if(album)
    {
        if(genre)
        {
            genre->addAlbum(album);
            album->addGenre(genre);
        }
        if(artist)
        {
            artist->addAlbum(album);
            album->addArtist(artist);
        }
        album->addTrack(track);
    }
    if(artist)
    {
        if(genre)
        {
            genre->addArtist(artist);
            artist->addGenre(genre);
        }
        artist->addTrack(track);
    }
    if(genre)
        genre->addTrack(track);
    sTrack.append(track);
}

Collection::~Collection()
{
    mutex.lock();
    abort = true;
    condition.wakeOne();
    mutex.unlock();
    wait();
}
